import { Matrix } from "./matrix";
import { Fence } from "./fence";

class FenceGroup {
  spu;
  skuList = [];
  fences = [];

  constructor(spu) {
    this.spu = spu;
    this.skuList = spu.sku_list;
  }

  getDefaultSku() {
    const defaultSkuId = this.spu.default_sku_id;
    if (!defaultSkuId) {
      return;
    }
    return this.skuList.find((s) => s.id === defaultSkuId);
  }

  getSku(skuCode) {
    const fullSkuCode = this.spu.id + "$" + skuCode;
    const sku = this.spu.sku_list.find((s) => s.code === fullSkuCode);
    return sku ? sku : null;
  }

  // 根据CellId，更新Cell各类状态
  setCellStatusById(cellId, status) {
    this._eachCell((cell) => {
      if (cell.id === cellId) {
        cell.status = status;
      }
    });
  }
  // 根据Cell x、y，更新Cell各类状态
  setCelStatusByXY(x, y, status) {
    this.fences[x].cells[y].status = status;
  }

  // 初始化
  initFences() {
    const matrix = this._createMatrix(this.skuList);
    // console.log("matrix", matrix);
    const fences = [];

    const AT = matrix.transpose();
    AT.forEach((r) => {
      const fence = new Fence(r);
      fence.init();
      if (this._hasSketchFence() && this._isSketchFence(fence.id)) {
        fence.setFenceSketch(this.skuList);
      }
      fences.push(fence);
    });
    this.fences = fences;
  }

  _hasSketchFence() {
    return this.spu.sketch_spec_id ? true : false;
  }

  _isSketchFence(fenceId) {
    return this.spu.sketch_spec_id === fenceId ? true : false;
  }

  _eachCell(cb) {
    for (let i = 0; i < this.fences.length; i++) {
      for (let j = 0; j < this.fences[i].cells.length; j++) {
        const cell = this.fences[i].cells[j];
        cb(cell, i, j);
      }
    }
  }
  //
  _createMatrix(skuList) {
    const m = [];
    skuList.forEach((sku) => {
      m.push(sku.specs);
    });
    return new Matrix(m);
  }
}

export { FenceGroup };
